package com.py.credit.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.QyRateNum;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.service.DynamicMapService;
import com.py.credit.utils.DynamicMapUtil;

@Controller
@RequestMapping("/dynamicMap")
public class DynamicMapController {

	@Autowired
	private DynamicMapService dynamicMapService;

	@RequestMapping("/index.html")
	public String index() {

		return "resourse/jsp/dynamicMap/index";

	}

	/** 各区域当年外贸企业进出口总额 **/

	@RequestMapping("/tradeVolume.json")
	@ResponseBody
	public RestServiceResult<Object> getTradeVolume(@RequestParam(value = "year", defaultValue = "", required = true) int year) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			r=new RestServiceResult<Object>(dynamicMapService.getTradeVolume(year)==null?new HashMap<String, Object>():dynamicMapService.getTradeVolume(year));
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;

	}

	/** 进出口额排名 **/

	@RequestMapping("/tradeVolumeRank.json")
	@ResponseBody
	public RestServiceResult<Object> getTradeVolumeRank(@RequestParam(value = "year", defaultValue = "", required = true) int year,
			@RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			Integer tradeVolumeRank = dynamicMapService.getTradeVolumeRank(year, section);
			Integer iTradeVolumeRank = dynamicMapService.getTradeVolumeRankByType(year, section, "I");
			Integer eTradeVolumeRank = dynamicMapService.getTradeVolumeRankByType(year, section, "E");

			map.put("I", iTradeVolumeRank);
			map.put("E", eTradeVolumeRank);
			map.put("T", tradeVolumeRank);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;

	}

	/** 商品进出口额前5 **/

	@RequestMapping("/productTradeVolume.json")
	@ResponseBody
	public RestServiceResult<Object> getProductTradeVolume(@RequestParam(value = "year", defaultValue = "", required = true) int year,
			@RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			List<Map<String, Object>> eProductTop5 = dynamicMapService.getProductTop5(year, section, "E");
			List<Map<String, Object>> iProductTop5 = dynamicMapService.getProductTop5(year, section, "I");
			map.put("I", iProductTop5);
			map.put("E", eProductTop5);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;
	}

	/** 商品进出口额前5 **/

	@RequestMapping("/latestHalfYearProductTradeVolume.json")
	@ResponseBody
	public RestServiceResult<Object> latestYearProductTradeVolume(
			@RequestParam(value = "section", defaultValue = "", required = true) String section,
			@RequestParam(value = "year", defaultValue = "", required = true) int year) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();

			List<Map<String, Object>> Ilist = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> Elist = new ArrayList<Map<String, Object>>();

			List<Map<String, Object>> eProductTop5 = dynamicMapService.getProductTop5(year, section, "E");
			List<Map<String, Object>> iProductTop5 = dynamicMapService.getProductTop5(year, section, "I");

			for (Map<String, Object> ep : eProductTop5) {

				String name = ep.get("name").toString();

				List<Map<String, Object>> data = dynamicMapService.getProductInfo(name, year, section, "E");
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("name",name);
				dataMap.put("list",data);
				Elist.add(dataMap);
			}

			for (Map<String, Object> ip : iProductTop5) {

				String name = ip.get("name").toString();

				List<Map<String, Object>> data = dynamicMapService.getProductInfo(name, year, section, "I");
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put("name",name);
				dataMap.put("list",data);
				Ilist.add(dataMap);
			}

			map.put("I", Ilist);
			map.put("E", Elist);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		

		return r;
	}

	/**
	 * 
	 * @param year
	 * @param section
	 * @return
	 */
	@RequestMapping("/getTradeCountryTop6.json")
	@ResponseBody
	public RestServiceResult<Object> getTradeCountryTop6(@RequestParam(value = "year", defaultValue = "", required = true) int year,
			@RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			List<Map<String, Object>> eCountryTop6 = dynamicMapService.getTradeCountryTop6(year, section, "E");
			List<Map<String, Object>> iCountryTop6 = dynamicMapService.getTradeCountryTop6(year, section, "I");
			map.put("I", iCountryTop6);
			map.put("E", eCountryTop6);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;
	}

	@RequestMapping("/eiCount.json")
	@ResponseBody
	public RestServiceResult<Object> getEiCount(@RequestParam(value = "year", defaultValue = "", required = true) int year,
							 @RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> dataExportMap = new HashMap<String, Object>();
			Map<String, Object> dataImportMap = new HashMap<String, Object>();

			for(int i=1;i<=12;i++){
				if (i < 10) {
					dataExportMap.put(year + "-0" + i,
							dynamicMapService.selectEITradeTendencyCount(year, i, "E", section));
					dataImportMap.put(year + "-0" + i,
							dynamicMapService.selectEITradeTendencyCount(year, i, "I", section));
				} else {
					dataExportMap.put(year + "-" + i,
							dynamicMapService.selectEITradeTendencyCount(year, i, "E", section));
					dataImportMap.put(year + "-" + i,
							dynamicMapService.selectEITradeTendencyCount(year, i, "I", section));
				}
			}

			map.put("exportCount", dataExportMap);
			map.put("importCount", dataImportMap);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		
		return r;
	}

	@RequestMapping("/dictTypeChartCount.json")
	@ResponseBody
	public RestServiceResult<Object> getDictTypeChartCount(@RequestParam(value = "year", defaultValue = "", required = true) int year,
			@RequestParam(value = "code", defaultValue = "", required = true) String code,
			@RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			List<Map<String, Object>> map = dynamicMapService.selectDictTypeChartCount(year, code, section);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;
	}

	@RequestMapping("/radarChartCount.json")
	@ResponseBody
	public RestServiceResult<Object> getRadarChartCount(@RequestParam(value = "year", defaultValue = "", required = true) Integer year,
									 @RequestParam(value = "type", defaultValue = "", required = true) String type,
									 @RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String,Object> map=new HashMap<String, Object>();
			List<QyRateNum> data = dynamicMapService.selectQyRateNum(year, 0, type, "", "", section);
			for (QyRateNum qyRateNum:data
				 ) {
				int maxNum=DynamicMapUtil.getMaxCount(qyRateNum);
				map.put("key",qyRateNum.getKey());
				map.put("value",qyRateNum.getValue());
				map.put("aaNum",qyRateNum.getAaNum());
				map.put("aNum",qyRateNum.getaNum());
				map.put("bNum",qyRateNum.getbNum());
				map.put("cNum",qyRateNum.getcNum());
				map.put("maxNum",maxNum);
			}
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		
		return r;
	}

	@RequestMapping("/totalCount.json")
	@ResponseBody
	public RestServiceResult<Object> getRadarChartCount(@RequestParam(value = "year", defaultValue = "", required = true) int year,
									 @RequestParam(value = "section", defaultValue = "", required = true) String section) {
		RestServiceResult<Object> r=new RestServiceResult<Object>();
		try{
			Map<String,Object> map=new HashMap<String, Object>();
			BigDecimal eTotalCount = dynamicMapService.selectEITotalCount(year, "E",section);
			BigDecimal iTotalCount = dynamicMapService.selectEITotalCount(year, "I",section);
			List<Map<String, Object>> cityTotalCount = dynamicMapService.selectCityTotalCount(year);
			List<Map<String, Object>> commodityTotalCount = dynamicMapService.selectCommodityTotalCount(year,section);
			map.put("eTotalCount",eTotalCount);
			map.put("iTotalCount",iTotalCount);
			map.put("cityTotalCount",cityTotalCount);
			map.put("commodityTotalCount",commodityTotalCount);
			r=new RestServiceResult<Object>(map);
		}catch(Exception e){
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("统计异常！");
		}
		return r;
	}
}
